/*
 * This file is part of Smart Route for S60 3rd Edition terminals
 * 
 * Copyright (C) 2006  Harri Salminen

 * This program free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "SmartRouteDocument.h"
#include "SmartRouteAppUi.h"

CSmartRouteDocument::CSmartRouteDocument( CEikApplication& anApplication )
	: CEikDocument( anApplication ),
	iOpacity(255),
	iColour(255,0,0),
	iLineWidth(KDefaultLineWidth),
	iFrequency(KDefaultUpdateFreq)
	{
	}

CSmartRouteDocument::~CSmartRouteDocument()
	{
	}
	

void CSmartRouteDocument::ConstructL()
	{
	}

CSmartRouteDocument* CSmartRouteDocument::NewL( CEikApplication& aApp )
	{
	CSmartRouteDocument* self = new (ELeave) CSmartRouteDocument( aApp );
	CleanupStack::PushL( self );
	self->ConstructL();
	CleanupStack::Pop( self );
	return self;
	}


CEikAppUi* CSmartRouteDocument::CreateAppUiL()
	{
	return new (ELeave) CSmartRouteAppUi;
	}
	
void CSmartRouteDocument::StoreL(CStreamStore &aStore, CStreamDictionary &aStreamDic) const
	{
	RStoreWriteStream stream;
    TStreamId id = stream.CreateLC(aStore);

    stream.WriteUint8L(iLineWidth);
    stream.WriteUint8L(iFrequency);
    stream << iColour;
    stream.CommitL();
    aStreamDic.AssignL(Application()->AppDllUid(), id);
    CleanupStack::PopAndDestroy(); // stream
	}
	
void CSmartRouteDocument::RestoreL(const CStreamStore &aStore, const CStreamDictionary &aStreamDic)
	{
	TStreamId id = aStreamDic.At(Application()->AppDllUid());
    RStoreReadStream stream;
    stream.OpenLC(aStore, id );
    iLineWidth = stream.ReadUint8L();
    iFrequency = stream.ReadUint8L();
    stream >> iColour;
    CleanupStack::PopAndDestroy();  // stream
	}
				
TReal CSmartRouteDocument::LineWidth()
	{
	return iLineWidth<KMaxLineWidth ? iLineWidth : KMaxLineWidth;
	}
	
TKmlColor CSmartRouteDocument::Color()
	{
	TKmlColor colour;
	colour.Format(_L("%02x%02x%02x%02x"), iOpacity, 
		iColour.Blue(), iColour.Green(), iColour.Red());
	return colour;
	}
